/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ygroup.eventsystem.integration.spring;

import com.ygroup.eventsystem.integration.spring.annotation.EventListenerAware;
import com.ygroup.eventsystem.registry.EventHandlerRegistry;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanPostProcessor;

/**
 *
 * @author joshua
 */
public class EventListenerBeanPostProcessor implements BeanPostProcessor
{
    private EventHandlerRegistry eventHandlerRegistry;

    public void setEventHandlerRegistry(EventHandlerRegistry eventHandlerRegistry)
    {
        this.eventHandlerRegistry = eventHandlerRegistry;
    }

    @Override
    public Object postProcessBeforeInitialization(Object o, String string) throws BeansException
    {
        Class clazz = o.getClass();
        if (clazz.isAnnotationPresent(EventListenerAware.class))
        {
            this.eventHandlerRegistry.registerEventHandler(o);
        }
        return o;
    }

    @Override
    public Object postProcessAfterInitialization(Object o, String string) throws BeansException
    {
        return o;
    }
}
